package cn.mxh.pethspt.dao.impl;

import cn.mxh.pethspt.dao.CombinationDao;
import cn.mxh.pethspt.entity.Combination;
import cn.mxh.pethspt.entity.Ding;
import cn.mxh.pethspt.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CombinationDaoImpl implements CombinationDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 添加套餐
     *
     * @return
     */
    @Override
    public int add(Combination combination) throws Exception {
        int num = 0;
        try{
            String sql = "insert into combination(coName,coIntroduce,coPrice)VALUES(?,?,?);";
            Object[] objects = new Object[]{
                    combination.getCoName(),
                    combination.getCoIntroduce(),
                    combination.getCoPrice()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 修改套餐
     *
     * @param combination
     * @return
     */
    @Override
    public int update(Combination combination) throws Exception {
        int num = 0;
        try{
            String sql = "update combination set coName = ? where coId = ?;";
            Object[] objects = new Object[]{
                    combination.getCoName(),
                    combination.getCoId()
            };
            num = queryRunner.update(DruidUtils.getConnection(),sql,objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据id删除套餐
     *
     * @param coId@return
     * @throws Exception
     */
    @Override
    public int delete(Integer coId) throws Exception {
        int num = 0;
        try{
            String sql = "delete from combination where coId = ?;";
            num = queryRunner.update(DruidUtils.getConnection(),sql,coId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 查询所有套餐
     *
     * @return
     */
    @Override
    public List<Combination> getAll() throws Exception {
        List<Combination> comList = new ArrayList<Combination>();
        try{
            String sql = "select * from combination;";
            comList = queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Combination>(Combination.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return comList;
    }

    /**
     * 根据id查询套餐价格
     *
     * @param coId@return
     */
    @Override
    public Combination getCombinationBycoId(Integer coId) throws Exception {
        Combination combination = new Combination();
        try{
            String sql = "select coPrice from combination where coId = 1;";
            combination = queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Combination>(Combination.class),coId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return combination;
    }
}
